package com.concerneyes.health;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.concerneyes.R;
import com.concerneyes.mine.TabActivity.MyInfo;
import com.concerneyes.mine.Task.GlassManImpl;
import com.concerneyes.mine.Task.GlassManListener;
import com.concerneyes.mine.model.MyGlassManModel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import common.AbsActivity;
import view.MyUniversalDialog;
import wheelview.ChangeBirthDialog;
import wheelview.DataChoose.DialogData;

import static com.concerneyes.R.layout.activity_add_user;

/**
 * Created by Administrator on 2017/6/30 0030.
 */

public class HealthAddUserActivity extends AbsActivity implements View.OnClickListener,
        GlassManListener, DialogData.OnChooseDataSuccessListener {

    private EditText mEtName;
    private RelativeLayout mRlName;
    private EditText mEtSex;
    private RelativeLayout mRlSex;
    private EditText mEtBirthday;
    private RelativeLayout mRlBirthday;
    private EditText mEtZhiye;
    private RelativeLayout mRlZhiye;
    private EditText mEtDrive;
    private RelativeLayout mRlDrive;
    private EditText mEtCom;
    private RelativeLayout mRlCom;
    private TextView bottom_save;

    private GlassManImpl impl;
    private boolean isMan = true;
    private String mYear, mMonth, mDay;
    private boolean isAdd;
    private int glassMan_position = 0;
    private int id = 0;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(activity_add_user);
        isAdd = getIntent().getExtras().getBoolean("isAdd");
        impl = new GlassManImpl(this, this);
        if (isAdd) {
            setTitle(R.string.add_user);
        } else {
            setTitle(R.string.edit_user);
            impl.listGlassMan();
            glassMan_position = getIntent().getExtras().getInt("glassMan_position");
        }
        initView();
    }

    private void initView() {
        mEtName = (EditText) findViewById(R.id.et_name);
        mRlName = (RelativeLayout) findViewById(R.id.rl_name);
        mRlName.setOnClickListener(this);
        mEtSex = (EditText) findViewById(R.id.et_sex);
        mRlSex = (RelativeLayout) findViewById(R.id.rl_sex);
        mRlSex.setOnClickListener(this);
        mEtBirthday = (EditText) findViewById(R.id.et_birthday);
        mRlBirthday = (RelativeLayout) findViewById(R.id.rl_birthday);
        mRlBirthday.setOnClickListener(this);
        mEtZhiye = (EditText) findViewById(R.id.et_zhiye);
        mRlZhiye = (RelativeLayout) findViewById(R.id.rl_zhiye);
        mRlZhiye.setOnClickListener(this);
        mEtDrive = (EditText) findViewById(R.id.et_drive);
        mRlDrive = (RelativeLayout) findViewById(R.id.rl_drive);
        mRlDrive.setOnClickListener(this);
        mEtCom = (EditText) findViewById(R.id.et_com);
        mRlCom = (RelativeLayout) findViewById(R.id.rl_com);
        mRlCom.setOnClickListener(this);
        bottom_save = (TextView) findViewById(R.id.bottom_save);
        bottom_save = (TextView) findViewById(R.id.bottom_save);
        bottom_save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_name:
                break;
            case R.id.rl_sex:
                dialogSet(true);
                break;
            case R.id.rl_birthday:
                Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH) + 1;
                int day = c.get(Calendar.DAY_OF_MONTH);
                ChangeBirthDialog mChangeBirthDialog = new ChangeBirthDialog(
                        HealthAddUserActivity.this);
                mChangeBirthDialog.setDate(year, month, day);
                mChangeBirthDialog.show();
                mChangeBirthDialog.setBirthdayListener(new ChangeBirthDialog.OnBirthListener() {

                    @Override
                    public void onClick(String year, String month, String day) {
                        mYear = year;
                        if (month.length() == 1) {
                            mMonth = "0" + month;
                        } else {
                            mMonth = month;
                        }
                        mDay = day;
                        mEtBirthday.setText(mYear + "-" + mMonth + "-" + mDay);
                    }
                });
                break;
            case R.id.rl_zhiye:
//                List<String> datas = new ArrayList<>();
//                datas.add("IT");
//                datas.add("Doctor");
//                datas.add("Driver");
//                dialogShow(datas, getResources().getString(R.string.my_zhiye));
                break;
            case R.id.rl_drive:
                List<String> driveDatas = new ArrayList<>();
                driveDatas.add(getResources().getString(R.string.often_drive));
                driveDatas.add(getResources().getString(R.string.un_often_drive));
                dialogShow(driveDatas, getResources().getString(R.string.drive_con));
                break;
            case R.id.rl_com:
                List<String> comDatas = new ArrayList<>();
                comDatas.add(getResources().getString(R.string.often));
                comDatas.add(getResources().getString(R.string.un_often));
                dialogShow(comDatas, getResources().getString(R.string.com_con));
                break;

            case R.id.bottom_save:
                String name = mEtName.getText().toString();
                String sex = mEtSex.getText().toString();
                String birthday = mEtBirthday.getText().toString();
                String job = mEtZhiye.getText().toString();
                String drives = mEtDrive.getText().toString();
                String iscom = mEtCom.getText().toString();
                boolean is_com;
                if (iscom.equals(getResources().getString(R.string.often))){
                    is_com = true;
                }else {
                    is_com = false;
                }
                impl.addGlassMan(isAdd, String.valueOf(id), mYear, mMonth, mDay, name, sex, drives, is_com, job);
                break;
        }
    }

    DialogData dialogData;

    private void dialogShow(List<String> datas, String title) {
        final int width = this.getWindowManager().getDefaultDisplay().getWidth() * 5 / 6;
        dialogData = new DialogData(this, this, width, datas, title);
        dialogData.dialogShow();
    }

    @Override
    public void glassManListSuccess(List<MyGlassManModel> model) {
        setGlassMan(model.get(glassMan_position));
    }

    private void setGlassMan(MyGlassManModel model) {
        if (model != null) {
            mEtBirthday.setText(model.getYear() + "-" + model.getMonth() + "-" + model.getDay());
            mEtSex.setText(model.getGender());
            mEtName.setText(model.getRealName());
            if (model.isIsUseComputer()) {
                mEtCom.setText(getResources().getString(R.string.often));
            } else {
                mEtCom.setText(getResources().getString(R.string.un_often));
            }
            mEtDrive.setText(model.getDriveFrequency());
            mEtZhiye.setText(model.getJob());
            mYear = model.getYear();
            mMonth = model.getMonth();
            mDay = model.getDay();
            id = model.getId();
        }
    }

    @Override
    public void glassManListFail() {

    }

    @Override
    public void deleteGlassManSuccess(List<MyGlassManModel> model) {

    }

    @Override
    public void deleteGlassManFail() {

    }

    @Override
    public void addGlassManSuccess() {
        finish();
    }

    @Override
    public void addGlassManFail() {

    }

    private void dialogSet(boolean isSex) {
        int width = this.getWindowManager().getDefaultDisplay().getWidth();
        int hight = this.getWindowManager().getDefaultDisplay().getHeight();
        final MyUniversalDialog dialog = new MyUniversalDialog(this);
        dialog.setCanceledOnTouchOutside(true);
        if (isSex) {
            dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_pay, null),
                    MyUniversalDialog.DialogGravity.CENTER, -2, 4 * width / 5);
        } else {
            dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_date, null),
                    MyUniversalDialog.DialogGravity.CENTER, -2, 5 * width / 6);
            TextView title = (TextView) dialog.findViewById(R.id.title);
            DatePicker data_picker = (DatePicker) dialog.findViewById(R.id.data_picker);
            RelativeLayout rl_title = (RelativeLayout) dialog.findViewById(R.id.rl_title);
            rl_title.setVisibility(View.GONE);
            title.setText(R.string.set_birthday);
            dialog.show();
            return;
        }
        RelativeLayout rl_man = (RelativeLayout) dialog.findViewById(R.id.rl_wechat);
        RelativeLayout rl_woman = (RelativeLayout) dialog.findViewById(R.id.rl_alipay);
        final ImageView iv_woman = (ImageView) dialog.findViewById(R.id.iv_alipay);
        final ImageView iv_man = (ImageView) dialog.findViewById(R.id.iv_wechat);

        final TextView tvChooseSex = (TextView) dialog.findViewById(R.id.tv_pay);
        final ImageView cancel = (ImageView) dialog.findViewById(R.id.pay_cancel);
        ImageView iv1 = (ImageView) dialog.findViewById(R.id.iv_1);
        ImageView iv2 = (ImageView) dialog.findViewById(R.id.iv_2);
        TextView tv1 = (TextView) dialog.findViewById(R.id.tv_1);
        TextView tv2 = (TextView) dialog.findViewById(R.id.tv_2);
        TextView title = (TextView) dialog.findViewById(R.id.title);
        TextView tv_pay_info = (TextView) dialog.findViewById(R.id.tv_pay_info);
        iv1.setImageDrawable(getResources().getDrawable(R.drawable.ico_male));
        iv2.setImageDrawable(getResources().getDrawable(R.drawable.ico_female));
        tv1.setText(R.string.man);
        tv2.setText(R.string.woman);
        title.setText(R.string.set_sex);
        tv_pay_info.setVisibility(View.GONE);
        dialog.show();
        rl_man.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                iv_woman.setImageResource(R.drawable.btn_unselected);
                iv_man.setImageResource(R.drawable.btn_selected);
                isMan = true;
            }
        });
        rl_woman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_woman.setImageResource(R.drawable.btn_selected);
                iv_man.setImageResource(R.drawable.btn_unselected);
                isMan = false;
            }
        });
        tvChooseSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMan) {
                    mEtSex.setText("男");
                } else {
                    mEtSex.setText("女");
                }
                dialog.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    @Override
    public void chooseDataSuccess(String data) {
        String titleStr = dialogData.getTitleStr();
        if (titleStr.equals(getResources().getString(R.string.my_zhiye))) {
            mEtZhiye.setText(data);
        } else if (titleStr.equals(getResources().getString(R.string.drive_con))) {
            mEtDrive.setText(data);
        } else if (titleStr.equals(getResources().getString(R.string.com_con))) {
            mEtCom.setText(data);
        }

    }

    @Override
    public void onChooseDataFail() {

    }
}
